<?php
namespace User\Controller;
use Admin\Controller\CommonController;
use User\Model\UserModel;
use Think\Upload;
use Think\Image;
class UserController extends CommonController {
	private $user_db;
	function _initialize(){
		parent::_initialize();
		$this->user_db = new UserModel();
	}
	/**
	 * 用户列表
	 * @param number $page
	 * @param number $rows
	 * @param array  $search
	 */
	function index($page=1, $rows=10, $search = array()){
		if (IS_POST){
			$map   = array();
			if ($search) {
				if ($search['name']){
					$map['phone|nickname'] = array('like','%'.$search['name'].'%');
				}
			}
			$limit = ($page - 1) * $rows . "," . $rows;
			$total = $this->user_db->where($map)->count();
			$list  = $total ? $this->user_db->public_list(0, $map, $limit) : array();
			foreach ($list as $key=>$value){
                $cardResult=M('user_card')->where(array('user_id'=>$value['uid']))->find();
                $cardname=M('card')->where(array('id'=>$cardResult['card_id']))->getField('name');
                $list[$key]['card']=$cardname.'  '.$cardResult['number'];
				//格式化时间
                $list[$key]['uuid'] = $value['uid'];
				$list[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);
			}
			$this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
		}else {
			$this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
			$this->display('list');
		}
	}
	/**
	 * 编辑
	 * @param unknown $uid
	 */
	function edit($uid=0){
		if (IS_POST) {
			$data = I('info');
			if ($this->user_db->where(array('uid'=>$uid))->save($data)){
				$this->success('编辑成功');
			}else {
				$this->error('编辑失败');
			}
		}else {
			$this->info = $this->user_db->public_list($uid, array('u.uid'=>$uid), 1);
			$this->rand = I('rand');
			$this->display('edit');
		}
	}
	/**
	 * 删除
	 * @param unknown $uid
	 */
	function delete($uid){
		if ($this->user_db->where(array('uid'=>$uid))->delete()){
			$this->success('删除成功');
		}else {
			$this->error('删除失败');
		}
	}
	/**
	 * 注册协议
	 */
	function regist(){
		if (IS_POST) {
			if (!trim(I('text'))) $this->error('请输入内容');
			if (S('User_regist', I('text'))) {
				$this->success('提交成功');
			}else {
				$this->error('提交失败');
			}
		}else {
			$this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
			$this->text = '';
			if (S('User_regist')) $this->text = S('User_regist');
			$this->display('regist');
		}
	}
	/**
	 * 帮助中心
	 */
	function help(){
		if (IS_POST) {
			if (!trim(I('text'))) $this->error('请输入内容');
			if (S('User_help', I('text'))) {
				$this->success('提交成功');
			}else {
				$this->error('提交失败');
			}
		}else {
			$this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
			$this->text = '';
			if (S('User_help')) $this->text = S('User_help');
			$this->display('help');
		}
	}
	/**
	 * 昵称相同
	 * @param unknown $email
	 */
	function public_checkNickname($nickname){
		if (I('default') == $nickname) {
			$this->error('昵称相同');
		}
		$exists = $this->user_db->where(array('nickname'=>$nickname))->field('nickname')->find();
		if ($exists) {
			$this->success('昵称存在');
		}else{
			$this->error('昵称不存在');
		}
	}
	/**
	 * 上传头像
	 * @param unknown $uid
	 */
	function public_uploadHead($uid=0){
		$data = upload('/Picture/avatar/', $uid, 'user');
		if (is_string($data)) {
			$this->ajaxReturn(array('info'=>$data, 'status'=>0));
		}else {
			$this->ajaxReturn(array('info'=>'上传成功', 'url'=>$data['0']['smallUrl'], 'status'=>1));
		}
	}

    /**
     * 用户充值
     * @param int $id
     */
	function scoreInc($id=0){
        $return = $this->user_db->scoreInc(I('uid'), I('score'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }

    /**
     * 用户减值
     * @param int $id
     */
    function scoreDec($id=0){
        $return = $this->user_db->scoreDec(I('uid'), I('score'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }

    /**
     * 充值
     * @param int $page
     * @param int $rows
     * @param array $search
     */
    function recharge($page=1, $rows=10, $search = array()){
        if (IS_POST){
            $map['type']=array('eq',1);
            $map['status']=array('eq',0);
            $limit = ($page - 1) * $rows . "," . $rows;

            $result = M('user_trade')->where($map)->order('create_time desc')->limit($limit)->select();
            $total=count($result);
            foreach ($result as $k=>$v){
                $result[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                $result[$k]['headsmall']=M('user')->where(array('uid'=>$v['uid']))->getField('headsmall');
                $result[$k]['phone']=M('user')->where(array('uid'=>$v['uid']))->getField('phone');
                $result[$k]['create_time']=date('Y-m-d H:i', $v['create_time']);
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$result));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('recharge');
        }
    }

    /**
     * 通过充值申请，并发送推送消息，type 4
     */
    function rechargePass(){
        $return=$this->user_db->rechargePass(I('id'));
        if($return['code']){
            $this->error($return['msg']);
        }else{
            $this->success($return['msg']);
        }
    }

    /**
     * 决绝充值申请
     */
    function rechargeNo(){
        $return=$this->user_db->rechargeNo(I('id'));
        if($return['code']){
            $this->error($return['msg']);
        }else{
            $this->success($return['msg']);
        }
    }
    /**
     * 用户提现申请成功 并且发送通知给用户 type=5
     * @param int $page
     * @param int $rows
     * @param array $search
     */
    function cashMoney($page=1, $rows=10, $search = array()){
        if (IS_POST){
            $map['type']=array('eq',2);
            $map['status']=array('eq',0);
            $limit = ($page - 1) * $rows . "," . $rows;

            $result = M('user_trade')->where($map)->order('create_time desc')->limit($limit)->select();
            $total=count($result);
            foreach ($result as $k=>$v){
                $result[$k]['nickname']=M('user')->where(array('uid'=>$v['uid']))->getField('nickname');
                $result[$k]['headsmall']=M('user')->where(array('uid'=>$v['uid']))->getField('headsmall');
                $cardR=M('user_card')->where(array('user_id'=>$v['uid']))->find();

               $carN=M('card')->where(array('id'=>$cardR['card_id']))->getField('name');
                $result[$k]['card']=$cardR['card_person'].'('.$carN.')'.$cardR['number'];
                $result[$k]['create_time']=date('Y-m-d H:i', $v['create_time']);
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$result));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('cashmoney');
        }
    }

    /**
     * 拒绝用户提现申请
     */
    function cashMoneyPass(){
        $return=$this->user_db->cashMoneyPass(I('id'));
        if($return['code']){
            $this->error($return['msg']);
        }else{
            $this->success($return['msg']);
        }
    }

    /**
     * 拒绝用户提现申请
     */
    function cashMoneyNo(){
        $return=$this->user_db->cashMoneyNo(I('id'));
        if($return['code']){
            $this->error($return['msg']);
        }else{
            $this->success($return['msg']);
        }
    }
}